layout.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { redirect } from "next/navigation";
  2. import Link from "next/link";
  3. import Image from "next/image";
  4. import { headers } from "next/headers";
  5. import { getI18n } from "locales/server";
  6. import Logo from "@public/logo.png";
  7. import { paths } from "@/shared/constants/paths";
  8. import { auth } from "@/features/auth/lib/better-auth";
  9. import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
  10. import type { LayoutParams } from "@/shared/types/next";
  11. export default async function AuthLayout(props: LayoutParams<{}>) {
  12. const t = await getI18n();
  13. const headerStore = await headers();
  14. const searchParams = Object.fromEntries(new URLSearchParams(headerStore.get("searchParams") || ""));
  15. const translatedError = t(`next_auth_errors.${searchParams.error}` as keyof typeof t);
  16. const user = await auth.api.getSession({ headers: headerStore });
  17. if (user) {
  18. redirect(`/${paths.dashboard}`);
  19. }
  20. return (
  21. <>
  22. <div>
  23. <div className="flex justify-center gap-2">
  24. <Link className="flex items-center gap-2 font-medium" href={`/${paths.dashboard}`}>
  25. <Image alt="workout cool logo" className="w-16" height={64} src={Logo} width={64} />
  26. </Link>
  27. </div>
  28. {searchParams.error && (
  29. <Alert className="mb-4" variant="error">
  30. <AlertTitle>{translatedError}</AlertTitle>
  31. <AlertDescription>{t("signin_error_subtitle")}</AlertDescription>
  32. </Alert>
  33. )}
  34. <div className="flex flex-1 items-center justify-center">
  35. <div className="w-full max-w-md">{props.children}</div>
  36. </div>
  37. </div>
  38. </>
  39. );
  40. }